// pages/my/myfriend/friend_list/friend_list.js
var app = getApp();
Page({
  data: {
    warmHeartList: [],
    currentUserInfo: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.user_id = options.user_id;
    this.setData({
      pageHeight: app.globalData.pageHeight,
      scrollListHeight: app.globalData.pageHeight,
    })
    this.getFriendDetail();
    this.getWarnHearListOptions();
    this.getAwardRecord();
  },

  //送他小星星
  sendStars: function () {
    if (this.data.currentUserInfo.is_giving) {
      this.IcanGetAwardOrNot();
    } else {
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=friend&act=giving',
        data: {
          friend_id: this.user_id
        },
        success: (res) => {
          this.IcanGetAwardOrNot();
        },
        fail: (err) => {
          this.setData({
            message: err,
          })
        }
      })
    }

  },

  getFriendDetail: function () {
    app.Ajax({
      url: '/api/index.php?app=friend&act=info',
      data: {
        friend_id: this.user_id
      },
      success: (res) => {
        this.setData({
          currentUserInfo: res,
        })
      },
      fail: (err) => {

      }
    })
  },

  getWarnHearListOptions: function () {
    var options = {
      list_id: 'warnHearList',
      url: '/api/index.php?app=friend&act=rank',
      data: {
        friend_id: this.user_id
      }
    }
    this.setData({
      options: options
    })
  },

  /**
   * 是否已领取登录奖励
   */
  getAwardRecord: function() {
    app.Ajax({
      url: '/api/index.php?app=account&act=tasks',
      success: (res) => {
        this.hasRegister = res.register.isCompleted;
      }
    })
  },

  /**
   * 判断我是否可以领取奖励
   */
  IcanGetAwardOrNot: function() {
    if (this.hasRegister) {
      this.setData({
        message: '您已领取过奖励了'
      })
      setTimeout(() => {
        wx.switchTab({
          url: '../../../homepage/homepage',
        })
      }, 2000);
    } else {
      wx.switchTab({
        url: '../../../homepage/homepage',
      })
    }
  },

  loadMoreData: function (e) {
    this.setData({
      warmHeartList: e.detail.data
    })
  },
})